1 00:00:00,510 --> 00:00:01,170 Welcome. 2 00:00:01,170 --> 00:00:06,150 In this lecture, we're going to explore how to let computers make decisions based on conditions in 3 00:00:06,150 --> 00:00:06,910 our code. 4 00:00:06,930 --> 00:00:10,320 If you don't know already, computers are incredibly stupid. 5 00:00:10,350 --> 00:00:15,450 They have absolutely no idea what to do unless you, a human, tells them what to do. 6 00:00:15,480 --> 00:00:20,880 Even if you make a mistake in your code, computers are incredibly obedient and will follow your commands 7 00:00:20,880 --> 00:00:22,290 even if they error. 8 00:00:22,320 --> 00:00:28,770 So in order to let a computer make a decision in programming, we get to use something called if statements. 9 00:00:28,800 --> 00:00:35,790 If statements check a condition, and if that condition evaluates to be true, then it executes a particular 10 00:00:35,790 --> 00:00:38,850 block of code according to that condition. 11 00:00:38,880 --> 00:00:45,660 So that means we can create an if statement by simply typing out the keyword in Lua, which is if and 12 00:00:45,660 --> 00:00:47,200 after we type out if. 13 00:00:47,220 --> 00:00:50,250 This is where we're going to pass our condition. 14 00:00:50,250 --> 00:00:54,540 So as an example, what I'm going to do is I'm going to create a variable up here real quick. 15 00:00:54,540 --> 00:00:57,030 And I'm just going to call this condition. 16 00:00:57,030 --> 00:01:00,360 And I'm going to set this variable equal to true. 17 00:01:00,360 --> 00:01:04,440 And then I can go ahead and pass this variable to my if statement. 18 00:01:04,440 --> 00:01:06,370 And we're going to pass the condition here. 19 00:01:06,390 --> 00:01:14,760 So if condition or this is basically saying if this condition is true then we want to do something specific. 20 00:01:14,760 --> 00:01:20,760 So afterwards when we are done checking the condition we need to type in the then keyword. 21 00:01:20,760 --> 00:01:22,500 And then we can hit enter. 22 00:01:22,500 --> 00:01:27,900 And as you can see automatically this other keyword appeared here which is the end keyword. 23 00:01:27,930 --> 00:01:29,580 What is the end keyword do. 24 00:01:29,610 --> 00:01:35,130 Well, the end keyword simply denotes the end of a particular scope in your code. 25 00:01:35,160 --> 00:01:39,810 Don't worry too much about scope for now, because we're going to be looking more into scope later. 26 00:01:39,810 --> 00:01:45,720 But basically when I created this if statement, it indented my code here and created this new block 27 00:01:45,720 --> 00:01:51,810 of code where I can go ahead and type other stuff in here that's going to execute, and it only exists 28 00:01:51,810 --> 00:01:57,930 within that particular block or this scope, and that scope ends at this end keyword. 29 00:01:57,930 --> 00:02:04,290 So this is just here to help the interpreter understand when a particular scope or a block of code ends. 30 00:02:04,410 --> 00:02:09,090 So what's going to happen here is the interpreter is going to go down line by line. 31 00:02:09,090 --> 00:02:10,590 And it's going to hit this if statement. 32 00:02:10,590 --> 00:02:17,190 And it's going to check whether or not the condition passed to this if statement is true or truthy. 33 00:02:17,220 --> 00:02:23,460 If it is, then it's going to hop into this particular scope or this block of code here, and it's going 34 00:02:23,460 --> 00:02:25,750 to execute whatever is inside of it. 35 00:02:25,770 --> 00:02:30,810 So that means I can put something like a print statement in here, and I can print out something like 36 00:02:30,810 --> 00:02:33,480 the condition was truthy. 37 00:02:33,750 --> 00:02:38,970 And the reason I'm calling it truthy is how the Lua interpreter evaluates conditions. 38 00:02:38,970 --> 00:02:43,980 So the interpreter only cares whether or not a condition exists or does not exist. 39 00:02:43,980 --> 00:02:49,050 So, for example, if I had another condition, I'm going to call this condition two and set it equal 40 00:02:49,050 --> 00:02:49,770 to nil. 41 00:02:50,070 --> 00:02:56,610 If I pass condition two here as the condition to my if statement, this block of code is not going to 42 00:02:56,610 --> 00:03:00,410 execute because condition two has no value. 43 00:03:00,420 --> 00:03:08,640 So any time a variable or something in your game is equal to false or nil, it's going to evaluate as 44 00:03:08,670 --> 00:03:09,390 false. 45 00:03:09,540 --> 00:03:16,920 Any lack of value means that it is false, or if it's directly set to false as a boolean data type, 46 00:03:16,920 --> 00:03:19,170 then it's going to evaluate to false as well. 47 00:03:19,200 --> 00:03:25,620 Right now, if we had our condition to equal to something like a number, our if statement doesn't care 48 00:03:25,620 --> 00:03:31,680 that it's a number, but it does care whether or not there is actually a value stored within this variable. 49 00:03:31,710 --> 00:03:37,320 Since there is a value associated with this variable, then it evaluates to be truthy. 50 00:03:37,320 --> 00:03:44,940 So any variables we have in our game that set as true, or it's set as a number or a string or a table 51 00:03:44,940 --> 00:03:51,240 or whatever the case is, it's going to evaluate as truthy, because a value exists there when a value 52 00:03:51,240 --> 00:03:57,810 does not exist there, or we explicitly set the value to be false, then it evaluates as false. 53 00:04:00,540 --> 00:04:03,960 I'm going to set condition two equal to nil. 54 00:04:04,700 --> 00:04:09,740 And then we're going to run the game and see if we get this statement printed out into the console. 55 00:04:09,950 --> 00:04:14,420 Now, this shouldn't get printed in the console at all because it's going to check condition two. 56 00:04:14,420 --> 00:04:20,540 And since condition two has no value, it's going to evaluate as false and nothing will appear in the 57 00:04:20,540 --> 00:04:20,990 console. 58 00:04:21,020 --> 00:04:22,880 So if we go ahead and hit run. 59 00:04:23,970 --> 00:04:29,080 As you can see, we got nothing at all inside of our console because condition two was Falsy. 60 00:04:29,820 --> 00:04:36,660 However, if I change condition to to be something like, for example, a table, even though there 61 00:04:36,660 --> 00:04:42,220 is nothing inside of this table, if I run the game, we're going to get printed out into the console. 62 00:04:42,240 --> 00:04:43,530 The condition was truthy. 63 00:04:43,560 --> 00:04:47,360 This is because a value was found inside of this variable. 64 00:04:47,370 --> 00:04:53,610 So again, the Lou interpreter doesn't care whether or not the value is a table or a number or whatever 65 00:04:53,610 --> 00:04:54,230 the case is. 66 00:04:54,240 --> 00:05:00,150 The only thing it's looking for is whether or not there is actually a value stored inside of that variable. 67 00:05:00,970 --> 00:05:06,730 Now, if we wanted to check, for example, the first condition and we set it equal to a number, and 68 00:05:06,730 --> 00:05:12,260 we want to only execute this if statement if our first condition is equal to the number ten. 69 00:05:12,280 --> 00:05:16,360 Well, what we can do is we can first refer to our condition variable. 70 00:05:16,360 --> 00:05:19,270 And then we're going to put two equal signs here. 71 00:05:19,300 --> 00:05:20,980 What are these two equal signs represent. 72 00:05:21,010 --> 00:05:25,600 Well this is known as the equality operator in Lua. 73 00:05:25,810 --> 00:05:28,240 We aren't assigning condition a value. 74 00:05:28,240 --> 00:05:33,130 Instead what we're doing is we're grabbing the value inside of our condition variable. 75 00:05:33,130 --> 00:05:37,060 And we're checking to see if it's equivalent to another value. 76 00:05:37,060 --> 00:05:42,260 In this case, we're going to check if the value inside of our condition variable is equal to ten. 77 00:05:42,280 --> 00:05:45,760 So this is a equivalency or equality operator. 78 00:05:45,760 --> 00:05:48,430 And this is not the assignment operator. 79 00:05:48,430 --> 00:05:51,550 So these are very two different operators in Lua. 80 00:05:51,580 --> 00:05:57,460 If we try and doing this we're going to get a red underline because it says expected. 81 00:05:57,460 --> 00:06:00,910 Then when parsing if statement got equal sign. 82 00:06:00,910 --> 00:06:05,530 So we're trying to override the value inside of our condition variable with ten. 83 00:06:05,530 --> 00:06:09,100 And we're doing it inside of an if statement condition. 84 00:06:09,100 --> 00:06:11,590 So we're encountering a weird error. 85 00:06:11,620 --> 00:06:16,630 Make sure to use the equality or the equivalent operator. 86 00:06:16,780 --> 00:06:19,750 Since our condition stores the value of ten. 87 00:06:19,750 --> 00:06:24,860 That means when it checks to see if that value is equal to ten, it's going to be true. 88 00:06:24,880 --> 00:06:31,510 So if we hit run, as you can see, we get the condition was truthy because the value stored in our 89 00:06:31,510 --> 00:06:33,520 variable was equal to ten. 90 00:06:34,240 --> 00:06:40,990 Now, another key word I want to introduce with if statements is something called the else keyword. 91 00:06:41,020 --> 00:06:48,700 So if I replace this end word here and I type out else and then I hit enter, we're going to get a new 92 00:06:48,700 --> 00:06:52,780 block of code right below our original block of code. 93 00:06:52,900 --> 00:06:56,650 So what this is going to do is it's going to check the condition up here. 94 00:06:56,770 --> 00:07:02,080 And if this condition evaluates to true it's going to execute this block of code up here. 95 00:07:02,200 --> 00:07:09,250 Otherwise if the condition up here did not evaluate to true, then it's going to execute this block 96 00:07:09,250 --> 00:07:10,900 down here instead. 97 00:07:10,930 --> 00:07:16,470 So in this block of code I can print something like the condition was false. 98 00:07:17,840 --> 00:07:23,120 And then if we want to check if the condition is equal to something like 20. 99 00:07:23,450 --> 00:07:25,190 And then we go and run the game. 100 00:07:25,870 --> 00:07:32,530 We're obviously going to get the statement the condition was false because ten is not equal to 20. 101 00:07:32,560 --> 00:07:33,160 Right. 102 00:07:34,270 --> 00:07:39,320 Now, let's say you wanted to have multiple different chunks inside of your if statement, right? 103 00:07:39,340 --> 00:07:40,690 Multiple different blocks of code. 104 00:07:40,690 --> 00:07:43,330 What if you wanted to check multiple different conditions? 105 00:07:43,330 --> 00:07:46,840 Well, what we could do is we could use another keyword in Lua. 106 00:07:46,840 --> 00:07:49,180 And this keyword is called else. 107 00:07:49,180 --> 00:07:56,950 If so, if I put an if here we now have the else if keyword and after this else if keyword we have to 108 00:07:56,950 --> 00:07:58,330 pass another condition. 109 00:07:58,330 --> 00:08:01,660 So what I'm going to do is I'm going to pass condition two here. 110 00:08:01,720 --> 00:08:05,350 And then after we do that we have to put another then keyword. 111 00:08:05,470 --> 00:08:11,350 And now what's going to happen here is it's going to first evaluate this condition. 112 00:08:11,350 --> 00:08:17,440 If this condition is true it's going to go into this block of code, execute and ignore the rest of 113 00:08:17,440 --> 00:08:18,490 the if statement. 114 00:08:18,520 --> 00:08:25,000 However if this condition evaluates to false, then it's going to hop to the next condition, which 115 00:08:25,000 --> 00:08:25,720 is condition two. 116 00:08:25,750 --> 00:08:27,430 It's going to check if this is true. 117 00:08:27,460 --> 00:08:32,620 If this condition is true, then it's going to go into this block of code and execute and ignore the 118 00:08:32,620 --> 00:08:34,420 rest of the if statement. 119 00:08:34,420 --> 00:08:40,750 However, if neither of these two conditions evaluates to true, then nothing should print in our console 120 00:08:40,750 --> 00:08:41,560 at all. 121 00:08:41,680 --> 00:08:48,160 So what I'm going to do here is I'm going to change the text inside of the second print statement. 122 00:08:48,160 --> 00:08:49,420 So I'm going to say. 123 00:08:50,200 --> 00:08:54,520 Condition two was truthy. 124 00:08:54,610 --> 00:08:58,750 And then up here I'm going to change this to condition was truthy. 125 00:08:59,650 --> 00:09:05,230 Now, since condition two stores a table, that obviously means it's going to evaluate to true. 126 00:09:05,260 --> 00:09:11,380 However, since condition or the value inside of our condition variable is equal to ten and it's not 127 00:09:11,380 --> 00:09:15,000 equal to 20, this is going to evaluate to be false, right. 128 00:09:15,070 --> 00:09:20,710 So if we hit run here we're going to get the statement condition two was truthy. 129 00:09:20,740 --> 00:09:22,420 Exactly what we expect. 130 00:09:22,570 --> 00:09:28,540 Of course if we would like to define any default behavior to happen in our script when neither of these 131 00:09:28,540 --> 00:09:33,070 conditions evaluate to true, then that's where we could use the else statement. 132 00:09:33,370 --> 00:09:39,970 So if this condition doesn't evaluate to true and this condition also does not evaluate to true, then 133 00:09:39,970 --> 00:09:44,530 this block of code is going to execute and we can print something like. 134 00:09:44,530 --> 00:09:48,940 Neither previous condition was truthy. 135 00:09:50,060 --> 00:09:54,980 Now, since condition two evaluates to be truthy, that means we're never going to be able to execute 136 00:09:54,980 --> 00:09:57,280 this else block of code here. 137 00:09:57,290 --> 00:10:00,920 However, if I change condition two to be nil. 138 00:10:01,160 --> 00:10:05,480 Well, of course we already know that ten is not equal to 20, so this is false. 139 00:10:05,480 --> 00:10:08,420 It skips this block of code goes to the next one. 140 00:10:08,420 --> 00:10:09,800 It checks condition two. 141 00:10:09,830 --> 00:10:13,340 Since condition two has no value then it evaluates to false. 142 00:10:13,340 --> 00:10:19,010 It skips this block of code and then it reaches the else statement which is the default behavior. 143 00:10:19,010 --> 00:10:20,540 And it's going to print out. 144 00:10:20,540 --> 00:10:23,200 Neither previous condition was truthy. 145 00:10:23,210 --> 00:10:24,500 So if we hit run. 146 00:10:25,450 --> 00:10:26,090 There we go. 147 00:10:26,110 --> 00:10:28,750 Neither previous condition was truthy. 148 00:10:28,900 --> 00:10:36,070 If we had, for example, changed the value inside of our condition variable equal to 20, then this 149 00:10:36,070 --> 00:10:40,540 is going to print out into the console, and neither of these ones are going to be printed out because 150 00:10:40,540 --> 00:10:42,610 they're going to be completely ignored. 151 00:10:42,640 --> 00:10:48,730 Because how this if statement is set up, it's only going to look for one condition that evaluates to 152 00:10:48,760 --> 00:10:55,950 true once it finds a condition that evaluates to true, the rest of our if statement is completely ignored. 153 00:10:55,960 --> 00:10:59,890 So that means if we hit run here, we should get there we go. 154 00:10:59,890 --> 00:11:04,660 Condition was truthy and we got nothing else printed inside of our console. 155 00:11:04,690 --> 00:11:07,750 Exactly what we'd expect for our if statement. 156 00:11:08,460 --> 00:11:15,510 So as a quick recap, any time we would like to check a values or variables or even properties inside 157 00:11:15,510 --> 00:11:21,090 of our game, and we want to execute a particular block of code based on a condition. 158 00:11:21,090 --> 00:11:26,760 This is where we would use if statements we put in the if keyword to start our if statement, and we 159 00:11:26,760 --> 00:11:32,550 pass a condition, and we can use the equivalency operator to check whether or not the value inside 160 00:11:32,550 --> 00:11:39,330 of this condition matches another specific value, or if we would just like to make sure that the value 161 00:11:39,330 --> 00:11:42,540 exists, then we can delete this completely. 162 00:11:42,540 --> 00:11:47,370 And what this does is it simply checks whether or not there is a value stored inside of this variable. 163 00:11:47,370 --> 00:11:52,590 If there's a value stored in there, it is truthy and it prints the condition was truthy. 164 00:11:52,620 --> 00:11:58,770 Otherwise, if this variable has no value inside of it, it evaluates to be false and it goes to check 165 00:11:58,770 --> 00:12:01,710 the next condition so it hits the next. 166 00:12:01,710 --> 00:12:07,920 Else if block and it checks the second condition, and if there's a value inside of this variable, 167 00:12:07,920 --> 00:12:09,000 then it's going to print. 168 00:12:09,030 --> 00:12:10,470 Hey, this was truthy. 169 00:12:10,500 --> 00:12:17,250 Otherwise, if no value was found inside of this condition, then it goes to the last block here, which 170 00:12:17,250 --> 00:12:18,450 is our else block. 171 00:12:18,450 --> 00:12:21,570 And the else block is used as the last resort. 172 00:12:21,570 --> 00:12:25,920 When every other previous condition did not evaluate to true. 173 00:12:26,280 --> 00:12:31,680 The else block does not need to check a condition because it doesn't care to check a condition. 174 00:12:31,680 --> 00:12:36,990 It only executes when every other previous condition to it was false. 175 00:12:37,320 --> 00:12:41,100 As an example, you could think of this as a weather monitor. 176 00:12:41,100 --> 00:12:46,230 When a weather monitor checks the weather, it tries to see if it's rainy or it's sunny, or if it's 177 00:12:46,230 --> 00:12:46,770 snowing. 178 00:12:46,770 --> 00:12:52,110 If the weather is rainy, then it's going to display rain clouds on the weather monitor. 179 00:12:52,140 --> 00:12:58,890 Otherwise, if it's overcast and it's going to display clouds, if it's sunny, it's going to display 180 00:12:58,890 --> 00:13:04,680 a sun and it's just going to keep on checking multiple different conditions until it reaches the end. 181 00:13:04,680 --> 00:13:10,230 So if all previous checks were not truthy, then the else block would set it to a default. 182 00:13:10,230 --> 00:13:12,570 So maybe the default would be it's sunny. 183 00:13:12,570 --> 00:13:15,390 So it goes through each of the different weather patterns. 184 00:13:15,390 --> 00:13:17,070 Okay, let's check to see if it's raining. 185 00:13:17,070 --> 00:13:18,720 Let's check to see if it's cloudy. 186 00:13:18,720 --> 00:13:20,160 Let's check to see if it's snowing. 187 00:13:20,160 --> 00:13:26,100 And if all of those checks did not evaluate to true, then the only option left would be hey, it's 188 00:13:26,100 --> 00:13:27,870 probably sunny outside. 189 00:13:28,140 --> 00:13:34,080 So this was a brief introduction into the power of if statements and letting computers in our game make 190 00:13:34,080 --> 00:13:34,830 decisions. 191 00:13:34,830 --> 00:13:39,450 And the next lecture I have an activity for you to complete so I will see you there.